package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemProject;
import com.wolfalpha.utron.utils.StringUtils;
import com.wolfalpha.utron.view.CircleImageView;

import java.util.List;

/**
 * 项目列表适配器
 * <p/>
 * Created by luodon on 15/8/28.
 */
public class ItemProjectAdapter extends BaseAdapter {


    private LayoutInflater inflater;

    private List<ItemProject> projects;

    private Context context;

    public ItemProjectAdapter(Context context, List<ItemProject> rs) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        projects = rs;
    }

    public void setData(List<ItemProject> rs) {
        if (rs != null) {
            projects = rs;
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return projects.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_project, null);
            viewHolder = new ViewHolder();
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.item_tv_project_name);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.item_tv_project_time);
            viewHolder.tvIntro = (TextView) convertView.findViewById(R.id.item_tv_project_intro);
            viewHolder.tvLocation = (TextView) convertView.findViewById(R.id.item_tv_project_location);
            viewHolder.tvDir = (TextView) convertView.findViewById(R.id.item_tv_project_dir);
            viewHolder.tvProgress = (TextView) convertView.findViewById(R.id.item_tv_project_progress);
            viewHolder.tvSchool = (TextView) convertView.findViewById(R.id.item_tv_project_school);
            viewHolder.ivLogo = (CircleImageView) convertView.findViewById(R.id.item_cv_project_logo);

            viewHolder.c1 = (TextView) convertView.findViewById(R.id.item_tv_project_c1);
            viewHolder.i1 = (ImageView) convertView.findViewById(R.id.item_iv_project_c1);
            viewHolder.c2 = (TextView) convertView.findViewById(R.id.item_tv_project_c2);
            viewHolder.i2 = (ImageView) convertView.findViewById(R.id.item_iv_project_c2);
            viewHolder.c3 = (TextView) convertView.findViewById(R.id.item_tv_project_c3);
            viewHolder.i3 = (ImageView) convertView.findViewById(R.id.item_iv_project_c3);
            viewHolder.c4 = (TextView) convertView.findViewById(R.id.item_tv_project_c4);
            viewHolder.i4 = (ImageView) convertView.findViewById(R.id.item_iv_project_c4);
            viewHolder.c5 = (TextView) convertView.findViewById(R.id.item_tv_project_c5);
            viewHolder.i5 = (ImageView) convertView.findViewById(R.id.item_iv_project_c5);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (projects != null) {
            //设置控件内容
            viewHolder.tvName.setText(projects.get(position).getName());
            viewHolder.tvTime.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(projects.get(position).getTime() * 1000)));
            viewHolder.tvIntro.setText(projects.get(position).getIntro());
            viewHolder.tvLocation.setText(String.valueOf(projects.get(position).getLocation()));
            viewHolder.tvSchool.setText(projects.get(position).getSchool());
            viewHolder.tvDir.setText(AppContext.getInstance().getDirs().get(projects.get(position).getDir() - 1000));
            viewHolder.tvProgress.setText(projects.get(position).getProgress());

//            if (viewHolder.c1.getTag() == null)
//                viewHolder.c1.setTag(projects.get(position).getId());
            String string = projects.get(position).getCalls();
            if (string != null) {
                if (string.contains("技术")) {
                    viewHolder.c1.setTextColor(Color.parseColor("#18b4ed"));
                    viewHolder.i1.setImageResource(R.mipmap.item_project_calls_tech_selected);
                } else {
                    viewHolder.c1.setTextColor(Color.parseColor("#c0c0c0"));
                    viewHolder.i1.setImageResource(R.mipmap.item_project_calls_tech_normal);
                }
                if (string.contains("运营")) {
                    viewHolder.c3.setTextColor(Color.parseColor("#18b4ed"));
                    viewHolder.i3.setImageResource(R.mipmap.item_project_calls_operation_selected);
                } else {
                    viewHolder.c3.setTextColor(Color.parseColor("#c0c0c0"));
                    viewHolder.i3.setImageResource(R.mipmap.item_project_calls_operation_normal);
                }
                if (string.contains("产品")) {
                    viewHolder.c5.setTextColor(Color.parseColor("#18b4ed"));
                    viewHolder.i5.setImageResource(R.mipmap.item_project_calls_product_selected);
                } else {
                    viewHolder.c5.setTextColor(Color.parseColor("#c0c0c0"));
                    viewHolder.i5.setImageResource(R.mipmap.item_project_calls_product_normal);
                }
                if (string.contains("设计")) {
                    viewHolder.c2.setTextColor(Color.parseColor("#18b4ed"));
                    viewHolder.i2.setImageResource(R.mipmap.item_project_calls_design_selected);
                } else {
                    viewHolder.c2.setTextColor(Color.parseColor("#c0c0c0"));
                    viewHolder.i2.setImageResource(R.mipmap.item_project_calls_design_normal);
                }
                if (string.contains("营销")) {
                    viewHolder.c4.setTextColor(Color.parseColor("#18b4ed"));
                    viewHolder.i4.setImageResource(R.mipmap.item_project_calls_sale_selected);
                } else {
                    viewHolder.c4.setTextColor(Color.parseColor("#c0c0c0"));
                    viewHolder.i4.setImageResource(R.mipmap.item_project_calls_design_normal);
                }
            }

            Picasso.with(context)
                    .load(projects.get(position).getLogo())
                    .placeholder(R.mipmap.project_default)
                    .into(viewHolder.ivLogo);
        }


        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvName;        //项目名
        private TextView tvSchool;      //起源地
        private TextView tvTime;        //更新时间
        private TextView tvIntro;       //一句话简介
        private TextView tvLocation;    //地点
        private TextView tvDir;         //方向
        private TextView tvProgress;    //阶段

        private CircleImageView ivLogo;       //LOGO

        private TextView c1;            //召唤类型1
        private ImageView i1;           //召唤类型图标1
        private TextView c2;            //召唤类型2
        private ImageView i2;           //召唤类型图标2
        private TextView c3;            //召唤类型3
        private ImageView i3;           //召唤类型图标3
        private TextView c4;            //召唤类型4
        private ImageView i4;           //召唤类型图标4
        private TextView c5;            //召唤类型5
        private ImageView i5;           //召唤类型图标5
    }
}
